﻿using System;
using System.Net.Mail;
using System.Text;
using NC.Data;

namespace NC.Models
{
    public class AccountUserModel : AccountUser
    {
        public void ActivateUser()
        {
            Active = true;
        }

        public void GenerateActivationCode()
        {
            var random = new Random();
            ActivationCode = string.Concat(FirstName, random.Next(NcConstants.RanNumMin, NcConstants.RanNumMax));
        }

        public void GeneratePassword()
        {
            var password = new Random();
            Password = string.Concat(FirstName, password.Next(NcConstants.RanNumMin, NcConstants.RanNumMax));
        }

        public MailMessage GetUserActivationMessage()
        {
            var emailTemplateService = new EmailTemplateRepository();
            var emailTemplate = emailTemplateService.GetEmailTemplateByName(TemplateName.UserActivation.ToString());
            var mail = new MailMessage(NcConstants.NcEmail, Email)
            {
                Body = string.Format(emailTemplate.EmailBody, NcConstants.ActivationUrl + ActivationCode),
                Subject = emailTemplate.Subject,
                IsBodyHtml = true,
                BodyEncoding = Encoding.GetEncoding("utf-8")
            };
            return mail;
        }

        /// <summary>
        ///     This email is sent when the user creats a post and we automatically create an account for them. we send this email
        ///     with password and activation link.
        /// </summary>
        /// <returns></returns>
        public MailMessage GetAutoUserCreationMessage()
        {
            var emailTemplateRepository = new EmailTemplateRepository();
            var emailTemplate =
                emailTemplateRepository.GetEmailTemplateByName(TemplateName.AutoUserCreationNActivation.ToString());
            var emailBody = string.Format(emailTemplate.EmailBody, NcConstants.ActivationUrl + ActivationCode, Email,
                Password);
            var mail = new MailMessage(NcConstants.NcEmail, Email)
            {
                Body = emailBody,
                Subject = emailTemplate.Subject,
                IsBodyHtml = true,
                BodyEncoding = Encoding.GetEncoding("utf-8")
            };
            return mail;
        }

        //password reset code
        public void GenerateResetPasswordCode()
        {
            var random = new Random();
            ResetPasswordCode = string.Concat(FirstName, random.Next(NcConstants.RanNumMin, NcConstants.RanNumMax));
        }

        public MailMessage GetPasswordActivationMessage()
        {
            var emailTemplateService = new EmailTemplateRepository();
            var emailTemplate = emailTemplateService.GetEmailTemplateByName(TemplateName.ResetPassword.ToString());
            var mail = new MailMessage(NcConstants.NcEmail, Email)
            {
                Body = emailTemplate.EmailBody + NcConstants.ResetPasswordUrl + ResetPasswordCode + "&userid=" + Id,
                Subject = emailTemplate.Subject,
                IsBodyHtml = true,
                BodyEncoding = Encoding.GetEncoding("utf-8")
            };
            return mail;
        }
    }
}